package com.ruoyi.teaching.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.teaching.domain.Classes;
import com.ruoyi.teaching.domain.StageInvestigateDetail;
import com.ruoyi.teaching.domain.Student;
import com.ruoyi.teaching.mapper.ClassesMapper;
import com.ruoyi.teaching.mapper.StageInvestigateDetailMapper;
import com.ruoyi.teaching.mapper.StudentMapper;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.StageInvestigateMapper;
import com.ruoyi.teaching.domain.StageInvestigate;
import com.ruoyi.teaching.service.IStageInvestigateService;

/**
 * 阶段问卷Service业务层处理
 *
 * @author sunshine
 * @date 2024-02-15
 */
@Service
public class StageInvestigateServiceImpl implements IStageInvestigateService
{
    @Autowired
    private StageInvestigateMapper stageInvestigateMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StageInvestigateDetailMapper stageInvestigateDetailMapper;

    /**
     * 查询阶段问卷
     *
     * @param id 阶段问卷主键
     * @return 阶段问卷
     */
    @Override
    public StageInvestigate selectStageInvestigateById(Long id)
    {
        return stageInvestigateMapper.selectStageInvestigateById(id);
    }

    /**
     * 查询阶段问卷列表
     *
     * @param stageInvestigate 阶段问卷
     * @return 阶段问卷
     */
    @Override
    public List<StageInvestigate> selectStageInvestigateList(StageInvestigate stageInvestigate)
    {
        List<StageInvestigate> stageInvestigates = stageInvestigateMapper.selectStageInvestigateList(stageInvestigate);

        return stageInvestigates;
    }



    /**
     * 新增阶段问卷
     *
     * @param stageInvestigate 阶段问卷
     * @return 结果
     */
    @Override
    public void insertStageInvestigate(StageInvestigate stageInvestigate)
    {
        stageInvestigate.setCreateTime(DateUtils.getNowDate());
        stageInvestigateMapper.insertStageInvestigate(stageInvestigate);

        Long id = stageInvestigate.getId();
        List<Student> studentList = studentMapper.selectStudentByStudentName(stageInvestigate.getClassName());
        for (Student s:studentList){
            StageInvestigateDetail stageInvestigateDetail = new StageInvestigateDetail();
            stageInvestigateDetail.setStage(stageInvestigate.getStage());
            stageInvestigateDetail.setMonth(stageInvestigate.getMonth());
            stageInvestigateDetail.setCreateBy(SecurityUtils.getUsername());
            stageInvestigateDetail.setCreateTime(new Date());
            stageInvestigateDetail.setStageInvestigateId(id);
            stageInvestigateDetail.setFeedbackStatus(2L);
            String name = s.getName();
            stageInvestigateDetail.setStudentName(name);
            stageInvestigateDetailMapper.insertStageInvestigateDetail(stageInvestigateDetail);
        }
    }

    /**
     * 修改阶段问卷
     *
     * @param stageInvestigate 阶段问卷
     * @return 结果
     */
    @Override
    public int updateStageInvestigate(StageInvestigate stageInvestigate)
    {
        return stageInvestigateMapper.updateStageInvestigate(stageInvestigate);
    }

    /**
     * 批量删除阶段问卷
     *
     * @param ids 需要删除的阶段问卷主键
     * @return 结果
     */
    @Override
    public int deleteStageInvestigateByIds(Long[] ids)
    {
        return stageInvestigateMapper.deleteStageInvestigateByIds(ids);
    }

    /**
     * 删除阶段问卷信息
     *
     * @param id 阶段问卷主键
     * @return 结果
     */
    @Override
    public int deleteStageInvestigateById(Long id)
    {
        return stageInvestigateMapper.deleteStageInvestigateById(id);
    }
}
